package com.gdevelop.examples.demo.client;


import com.gdevelop.gwtxp.client.GwtXP;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.i18n.client.HasDirection;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.LazyPanel;
import com.google.gwt.user.client.ui.Widget;

public class ContentWidget extends LazyPanel implements
    SelectionHandler<Integer>{
  
  private String name;

  private DecoratedTabPanel tabPanel;
  private boolean sourceLoaded;
  private HTML sourceWidget;
  private static String loadingImage;

  public ContentWidget(String name) {
    this.name = name;
  }

  protected Widget createWidget() {
    sourceWidget = new HTML("");
    
    tabPanel = new DecoratedTabPanel();
    tabPanel.setAnimationEnabled(true);
    tabPanel.setWidth("100%");
    tabPanel.addSelectionHandler(this);
    GwtXP gwtxp = Demo.controllerMap.get(Demo.GWTXP_XML_PACKAGE + name + ".gwtxp.xml");
    tabPanel.add((Widget)gwtxp.getGUI(), Demo.constants.example());
    tabPanel.add(sourceWidget, Demo.constants.source());

    return tabPanel;
  }

  @Override
  protected void onLoad() {
    ensureWidget();

    // Select the first tab
    if (tabPanel.getTabBar().getTabCount() > 0) {
      tabPanel.selectTab(0);
    }
  }

  public void onSelection(SelectionEvent<Integer> event) {
    int tabIndex = event.getSelectedItem().intValue();

    String tabHTML = tabPanel.getTabBar().getTabHTML(tabIndex);
    if (!sourceLoaded && tabHTML.equals(Demo.constants.source())) {
      sourceLoaded = true;
      requestSourceContents(DemoConstants.DST_SOURCE_EXAMPLE + name
          + ".gwtxp.xml.html", sourceWidget, null);
    }
  }

  static void requestSourceContents(String url, final HTML target,
      final RequestCallback callback) {
    // Show the loading image
    if (loadingImage == null) {
      loadingImage = "<img src=\"" + GWT.getModuleBaseURL()
          + "images/loading.gif\">";
    }
    target.setDirection(HasDirection.Direction.LTR);
    DOM.setStyleAttribute(target.getElement(), "textAlign", "left");
    target.setHTML("&nbsp;&nbsp;" + loadingImage);

    // Request the contents of the file
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
        GWT.getModuleBaseURL() + url);
    RequestCallback realCallback = new RequestCallback() {
      public void onError(Request request, Throwable exception) {
        target.setHTML("Cannot find resource");
        if (callback != null) {
          callback.onError(request, exception);
        }
      }

      public void onResponseReceived(Request request, Response response) {
        target.setHTML(response.getText());
        if (callback != null) {
          callback.onResponseReceived(request, response);
        }
      }
    };
    builder.setCallback(realCallback);

    // Send the request
    Request request = null;
    try {
      request = builder.send();
    } catch (RequestException e) {
      realCallback.onError(request, e);
    }
  }
}
